<?php

namespace apps\marriage\app\mp\controller;

use apps\marriage\core\model\MemberProfileExchangeApply;
use apps\marriage\core\service\MemberProfileExchangeApplyService;
use apps\marriage\core\service\MemberProfileService;
use apps\marriage\core\service\UserService;
use think\Exception;
use think\Request;

class MemberProfileExchangeApplyController extends BaseMarriageMpController
{
    /**
     * @var MemberProfileExchangeApplyService
     */
    private $memberProfileExchangeApplyService;

    /**
     * @var UserService
     */
    private $userService;

    /**
     * @var MemberProfileService
     */
    private $memberProfileService;

    protected function init()
    {
        parent::init();
        $this->memberProfileExchangeApplyService = service('MemberProfileExchangeApply', SERVICE_NAMESPACE);
        $this->userService = service('User', SERVICE_NAMESPACE);
        $this->memberProfileService = service('MemberProfile', SERVICE_NAMESPACE);
    }

    public function lists(Request $request)
    {
        $type = $request->param('type');
        $params = [];
        if($type == 'others') {
            $params['member_uid'] = $this->user['id'];
        } else if($type == 'my') {
            $params['apply_uid'] = $this->user['id'];
        }
        $res = $this->memberProfileExchangeApplyService->pageListByParams($params, 10);
        foreach ($res['data']as &$rs) {
            $rs = $this->memberProfileExchangeApplyService->format($rs);
            if($type == 'others') {
                $rs['apply_user'] = $this->userService->getByPk($rs['apply_uid']);
                $rs['apply_user'] = arrayOnly($rs['apply_user'], ['id', 'nick', 'avatar_url']);
                $rs['apply_member'] = $this->memberProfileService->getByUserId($rs['apply_uid']);
            } else if($type == 'my') {
                $rs['apply_to_user'] = $this->userService->getByPk($rs['member_uid']);
                $rs['apply_to_user'] = arrayOnly($rs['apply_to_user'], ['id', 'nick', 'avatar_url']);
                $rs['apply_to_member'] = $this->memberProfileService->getByUserId($rs['member_uid']);
            }
        }

        return $this->success($res);
    }

    public function audit(Request $request)
    {
        $id = $request->param('id', 0, 'intval');
        $status = $request->param('status');
        if($status == 'pass') {
            $status = MemberProfileExchangeApply::STATUS_PASS;
        } else if($status == 'reject') {
            $status = MemberProfileExchangeApply::STATUS_REJECT;
        } else {
            throw new Exception('参数错误');
        }
        if(!$this->memberProfileExchangeApplyService->info([
            'member_uid' => $this->user['id'],
            'id' => $id
        ])) {
            throw new Exception('无权操作');
        }
        $this->memberProfileExchangeApplyService->onAudit($id, $status);
        $apply = $this->memberProfileExchangeApplyService->getByPk($id);
        $apply = $this->memberProfileExchangeApplyService->format($apply);
        $apply['apply_user'] = $this->userService->getByPk($apply['apply_uid']);
        $apply['apply_user'] = arrayOnly($apply['apply_user'], ['id', 'nick', 'avatar_url']);
        return $this->success(['apply' => $apply]);
    }
}